<template>
    <div style="height: calc(100% - 40px)">
        <div class="select-form">
            <el-form :inline="true" :model="seleectQuery" class="demo-form-inline" @submit.native.prevent>
                <el-form-item label="景点名称" class="select-form-item">
                    <el-input v-model="seleectQuery.name" placeholder="景点名称" :clearable="true"></el-input>
                </el-form-item>
                <el-form-item label="景点类型" class="select-form-item">
                    <el-select v-model="seleectQuery.lx" placeholder="景点类型" :clearable="true">
                        <template v-for="(item, index) in jdlx">
                            <el-option :label=item.cFl :value=item.cId></el-option>
                        </template>
                    </el-select>
                </el-form-item>
                <el-form-item class="select-form-item">
                    <el-button type="primary" @click="select" native-type="submit">查询</el-button>
                    <el-button type="success" @click="add">添加</el-button>
                </el-form-item>
            </el-form>
        </div>
        <!-- 114px 是搜索内容-->
        <div style="height:calc(100% - 114px)">
            <!-- 32px 是页签高度 20px是页签边距-->
            <div style="height:100%; max-height:842px;">
                <el-table :border="true" :data="tableData" :stripe="true" height="100%" class="table"
                    :row-style="{ height: '80px' }" empty-text="暂无数据" :show-overflow-tooltip="true">
                    <el-table-column prop="cName" label="景点名称" min-width="15%" align="center">
                    </el-table-column>
                    <el-table-column prop="cTag" label="景点数量" min-width="10%" align="center">
                    </el-table-column>
                    <el-table-column label="景点类型" min-width="10%" align="center">
                        <template v-slot="slot">
                            {{ getJdlx(slot.row.cJdlxId) }}
                        </template>
                    </el-table-column>
                    <el-table-column label="封面图" min-width="10%" align="center">
                        <template v-slot="slot">
                            <el-image style="height: 80px" :src="slot.row.cCover" :preview-src-list="[slot.row.cCover]"
                                :preview-teleported="true" />
                        </template>
                    </el-table-column>
                    <el-table-column prop="dPrice" label="景点价格" min-width="10%" align="center">
                    </el-table-column>
                    <el-table-column prop="cDetail" label="景点描述" min-width="20%" align="center">
                    </el-table-column>
                    <el-table-column label="操作" min-width="10%" align="center">
                        <template v-slot="slot">
                            <el-button type="primary" @click="updateSp(slot.row)">编辑</el-button>
                            <el-button type="danger" @click="deleteSp(slot.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <el-pagination background layout="prev, pager, next" :total=total :page-size="10" class="pagination" />
        </div>


        <!-- 弹出层 新建景点或者是修改-->
        <el-dialog :title="isAdd ? '添加景点' : '修改景点'" v-model="showDialog" class="dialog-sp" :close-on-press-escape="false"
            :lock-scroll="true" :close-on-click-modal="false" :destroy-on-close="true">
            <el-scrollbar max-height="740px">
                <el-form :model="form" :rules="rules" ref="form" label-position="top">
                    <div style="display: flex;">
                        <el-form-item label="景点名称" prop="cName" style="flex: 1;">
                            <el-input v-model="form.cName" placeholder="请输入景点名称" clearable class="input"></el-input>
                        </el-form-item>
                        <el-form-item label="景点标签" prop="cTag" style="flex: 1;">
                            <el-input v-model="form.cTag" placeholder="请输入景点标签" clearable class="input"></el-input>
                        </el-form-item>
                    </div>

                    <div style="display: flex;">
                        <el-form-item label="封面图" prop="cCover" style="flex: 1;">
                            <el-upload class="avatar-uploader" :show-file-list="false"
                                :http-request="(parms) => uploadImg(parms, true)" accept="image/*">
                                <img v-if="form.cCover" :src="form.cCover" class="avatar" />
                                <el-icon v-else class="avatar-uploader-icon">
                                    <Plus />
                                </el-icon>
                            </el-upload>
                        </el-form-item>
                        <el-form-item label="景点价格" prop="dPrice" style="flex: 1;">
                            <el-input v-model="form.dPrice" placeholder="请输入景点价格" clearable class="input"></el-input>
                        </el-form-item>
                    </div>

                    <div style="display: flex;">
                        <el-form-item label="景点类型" prop="cJdlxId" style="flex: 1;">
                            <el-select v-model="form.cJdlxId" placeholder="景点类型" :clearable="true" class="select">
                                <template v-for="(item, index) in jdlx">
                                    <el-option :label="item.cFl" :value="item.cId"></el-option>
                                </template>
                            </el-select>
                        </el-form-item>

                        <el-form-item label="景点地址" prop="cAddress" style="flex: 1;">
                            <el-input v-model="form.cAddress" placeholder="请输入景点地址" clearable class="input"></el-input>
                        </el-form-item>
                    </div>

                    <div style="display: flex;">
                        <el-form-item label="经度" prop="cLongitude" style="flex: 1;">
                            <el-input v-model="form.cLongitude" placeholder="请输入经度" clearable class="input"></el-input>
                        </el-form-item>
                        <el-form-item label="纬度" prop="cLatitude" style="flex: 1;">
                            <el-input v-model="form.cLatitude" placeholder="请输入纬度" clearable class="input"></el-input>
                        </el-form-item>
                    </div>

                    <el-form-item label="景点描述" prop="cDetail">
                        <div style="width: 100%;">
                            <TEditor ref="editor" v-model:tinymceHtml="form.cDetail" :height="600" />
                        </div>
                    </el-form-item>
                </el-form>
            </el-scrollbar>
            <div slot="footer" class="dialog-footer">
                <el-button @click="showDialog = false">取 消</el-button>
                <el-button type="primary" @click="submitFl">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>
<style scoped>
.select-form {
    margin-top: 20px;
    margin-bottom: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid;
    border-radius: 10px;
    border-color: #ddd;
}

::v-deep .select-form-item {
    margin-top: 20px;
    margin-bottom: 20px;
}

::v-deep .table {
    border: 1px solid;
    border-radius: 10px;
    border-color: #ddd;
    max-height: "100%";
}

::v-deep .pagination {
    margin-top: 10px;
    float: right;
}

::v-deep .select,
.input {
    width: 80%;
}


::v-deep .dialog-footer {
    margin-top: 10px;
}

::v-deep .dialog-sp {
    margin-top: 2%;
    width: 800px;
    height: 900px;
    /* max-height: 80%; */
}

::v-deep .el-scrollbar__view {
    padding-right: 10px;
    height: 100%;
}
</style>
<script scoped>

import { Plus, UploadFilled } from '@element-plus/icons-vue'
import globalConfig from '../config'
import { loadingInstance } from '../utils/util';
import TEditor from '@/components/TEditor.vue';
export default {
    components: {
        Plus,
        UploadFilled,
        TEditor
    },


    data() {
        /**
         * 上传文件校验
         * @param {*} rule 
         * @param {*} value 
         * @param {*} callback 
         */
        var checkUpImage = (rule, value, callback) => {
            if (this.form.cCover != null && this.form.cCover != '') {
                return callback();
            } else {
                return callback(new Error('请上传封面图'));

            }
        };
        return {
            uploadUrl: globalConfig.url + '/api/file/imgUpload',

            seleectQuery: {
                // 景点名称
                name: '',
                // 景点类型
                lx: '',
            },
            tableData: [

            ],
            // 景点类型
            jdlx: [],
            // 景点数量
            total: 0,
            // 是否为添加页面
            isAdd: true,
            // 弹出层
            showDialog: false,
            // 提交表单
            form: {

            },
            // 校验规则
            rules: {
                cName: [
                    {
                        required: true,
                        message: '请输入景点名称',
                        trigger: 'blur'
                    }

                ],
                cTag: [
                    {
                        required: true,
                        message: '请输入景点标签',
                        trigger: 'blur'
                    }

                ],
                dPrice: [
                    {
                        required: true,
                        trigger: 'blur',
                        message: '请输入景点价格'
                    }
                ],
                cJdlxId:[
                    {
                        required: true,
                        trigger: 'blur',
                        message: '请选择景点类型'
                    }
                ],
                nSytj: [
                    {
                        required: true,
                        trigger: 'blur',
                        message: '请选择是否首页推荐'
                    }
                ],
                cJdlxId: [
                    {
                        required: true,
                        trigger: 'blur',
                        message: '请选择景点类型'
                    }
                ],
                cCover: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkUpImage
                    }
                ],
                cAddress:[
                    {
                        required: true,
                        trigger: 'blur',
                        message: '请输入景点地址'
                    }
                ],
                cLongitude:[
                    {
                        required: true,
                        trigger: 'blur',
                        message: '请输入经度'
                    }
                ],
                cLatitude:[
                    {
                        required: true,
                        trigger: 'blur',
                        message: '请输入纬度'
                    }
                ],
                cDetail:[
                    {
                        required: true,
                        trigger: 'blur',
                        message: '请输景点描述'
                    }
                ]
            },
            // 页数
            current: 1,
        }
    },
    beforeMount() {
        var that = this;
        // 渲染之前执行脚本
        // 获取景点类别
        this.$axios({
            method: 'POST',
            url: '/api/jdlx/v1/wx/getJdlx',
            data: {}
        })
            .then(res => {
                that.jdlx = res.data;
                // 获取景点
                that.selectFormDb();
            })
            .catch(err => {
                debugger
            })
    },
    methods: {
        // 查询
        select() {
            // 重置页数
            this.current = 1;
            this.selectFormDb();
        },
        // 查询提交方法
        selectFormDb() {
            var loding = loadingInstance(null, '数据加载中');
            this.$axios({
                url: 'api/spots/v1/select',
                data: {
                    // 当前页数
                    current: this.current,
                    // 景点名称
                    name: this.seleectQuery.name,
                    // 景点类型
                    jdfl: this.seleectQuery.lx,
                }


            })
                .then(res => {
                    this.tableData = res.data.data.list
                    this.total = res.data.data.total
                })
                .catch(err => {
                    debugger
                })
                .finally(fil => {
                    loding.close();
                })
        },

        // 添加景点按钮事件
        add() {
            this.isAdd = true;
            this.form = {};
            this.showDialog = true;
        },
        // 删除操作
        deleteSp(param) {
            var loding = loadingInstance('this', '删除中');

            // 发起请求
            this.$axios({
                url: 'api/spots/v1/del',
                data: param
            })
                .then(res => {
                    this.$message({
                        type: 'success',
                        message: '删除成功'
                    })
                    // 关闭当前弹出层
                    this.showDialog = false;
                    // 刷新当前数据
                    this.selectFormDb();
                })
                .catch(err => {
                    this.$message({
                        type: 'error',
                        message: '出现错误，请重试'
                    })
                })
                .finally(fil => {
                    loding.close();
                })
        },

        // 添加确认事件
        submitFl() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    var loding = loadingInstance('.dialog-sp', '提交中');
                    // 编辑form 请求数据
                    // var imgs = this.form.imgs;
                    // var newImgs = [];
                    if (this.form.imgs) {
                        for (var i = 0; i < this.form.imgs.length; i++) {
                            this.form.imgs[i].url = this.form.imgs[i].response
                        }
                    }
                    // 发起请求
                    this.$axios({
                        url: 'api/spots/v1/saveOrUpdate',
                        data: this.form
                    })
                        .then(res => {
                            this.$message({
                                type: 'success',
                                message: '提交成功'
                            })
                            // 关闭当前弹出层
                            this.showDialog = false;
                            // 刷新当前数据
                            this.selectFormDb();
                        })
                        .catch(err => {
                            this.$message({
                                type: 'error',
                                message: '出现错误，请重试'
                            })
                        })
                        .finally(fil => {
                            loding.close();
                        })
                }
                else {
                    this.$message({
                        message: "请检查必填项",
                        type: "error"
                    });
                }
            })
        },
        // 上传图片
        uploadImg(param, isfm) {
            return new Promise((resolve, reject) => {
                let formData = new FormData();
                formData.append('file', param.file)
                // 开启遮罩
                var loding = loadingInstance('this', '图片上传中');
                this.$axios({
                    url: this.uploadUrl,
                    method: 'POST',
                    header: { "Content-Type": "multipart/form-data" },
                    data: formData
                })
                    .then(res => {
                        if (isfm) {
                            this.form.cCover = res.data.data.url
                            resolve();
                        }
                        else {
                            resolve(res.data.data.url);
                        }

                    })
                    .catch(err => {
                        debugger
                        this.$message({
                            type: 'error',
                            message: '上传文件异常'
                        })
                        reject();
                    })
                    .finally(fil => {
                        loding.close();
                    })
            });
        },
        // 获取景点类型
        getJdlx(lxId) {
            this.jdlx.forEach(item => {

            })
            for (var i = 0; i < this.jdlx.length; i++) {
                if (lxId == this.jdlx[i].cId) {
                    return this.jdlx[i].cFl;
                }
            }
            return '';
        },

        // 编辑景点按钮
        updateSp(param) {
            this.isAdd = false;
            this.form = param
            this.showDialog = true;
        }
    }
}
</script>